package cn.flying.cloud.task.controller;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.system.common.service.CodeService;
import cn.flying.cloud.task.entity.TaskJobInfo;
import cn.flying.cloud.task.service.TaskJobInfoService;

/**
 * 定时任务配置信息 前端控制器
 *
 * @author: admin
 * @date: 2025-04-27 16:16:45
 * @version: 1.0
 */
@Controller
@RequestMapping("/task/job/info")
public class TaskJobInfoController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private CodeService codeService;
    @Resource
    private TaskJobInfoService taskJobInfoService;

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("index")
    public String index(Model model) {
        Map<String, String> statusList = codeService.getCodeList("status");
        model.addAttribute("statusList", statusList);
        return "task/job/index";
    }

    /**
     * 分页查询定时任务配置信息数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    @ResponseBody
    public PageResponse<TaskJobInfo> page(PageRequest<TaskJobInfo> request) {
        return taskJobInfoService.getPageList(request);
    }

    /**
     * 列表查询定时任务配置信息数据
     *
     * @param taskJobInfo
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    public Rt<?> list(@RequestBody TaskJobInfo taskJobInfo) {
        List<TaskJobInfo> list = taskJobInfoService.getList(taskJobInfo);
        return Rt.success(list);
    }

    /**
     * 查询定时任务配置信息详情数据
     *
     * @param taskJobInfo
     * @return
     */
    @PostMapping("detail")
    @ResponseBody
    public Rt<?> detail(TaskJobInfo taskJobInfo) {
        return Rt.success(taskJobInfoService.getOneObj(taskJobInfo));
    }

    /**
     * 根据主键ID查询定时任务配置信息详情数据
     *
     * @param id 主键ID
     * @return
     */
    @PostMapping("getById")
    @ResponseBody
    public Rt<?> getById(Long id) {
        TaskJobInfo taskJobInfo = taskJobInfoService.getById(id);
        return Rt.success(taskJobInfo);
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("turn/edit")
    public String turnEdit(Long id, Model model) {
        if (null != id) {
            TaskJobInfo formData = taskJobInfoService.getById(id);
            model.addAttribute("formData", formData);
        } else {
            TaskJobInfo formData = new TaskJobInfo();
            formData.setStatus("Y");
            model.addAttribute("formData", formData);
        }
        return "task/job/edit";
    }

    /**
     * 保存定时任务配置信息数据
     *
     * @param taskJobInfo
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    public Rt<?> save(@RequestBody TaskJobInfo taskJobInfo) {
        if (Objects.isNull(taskJobInfo.getId())) {
            // 新增
            taskJobInfoService.createObj(taskJobInfo);
        } else {
            // 更新
            taskJobInfoService.updateObj(taskJobInfo);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除定时任务配置信息数据
     *
     * @param taskJobInfo
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public Rt<?> delete(TaskJobInfo taskJobInfo) {
        taskJobInfoService.deleteObj(taskJobInfo);
        return Rt.success("删除成功！");
    }

    /**
     * 根据主键ID删除定时任务配置信息数据
     *
     * @param id 主键ID
     * @return
     */
    @GetMapping("deleteById/{id}")
    @ResponseBody
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        taskJobInfoService.deleteById(id);
        return Rt.success("删除成功！");
    }

    /**
     * 启动任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("start")
    public Rt<?> startJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup) {
        taskJobInfoService.startJob(jobName, jobGroup);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】启动成功！");
    }


    /**
     * 暂停任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("pause")
    public Rt<?> pauseJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup) {
        taskJobInfoService.pauseJob(jobName, jobGroup);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】暂停运行！");
    }

    /**
     * 恢复任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("resume")
    public Rt<?> resumeJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup) {
        taskJobInfoService.resumeJob(jobName, jobGroup);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】恢复运行！");
    }

    /**
     * 停止任务（表示完全不用了，下线了，如需重新启用，需要使用如下方法重新调度：）
     * // 重新添加作业和触发器
     * scheduler.scheduleJob(job, trigger);
     * // 重新启动调度器
     * scheduler.start();
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("stop")
    public Rt<?> stopJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup) {
        taskJobInfoService.stopJob(jobName, jobGroup);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】停止运行！");
    }

    /**
     * 立即触发执行一次任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("trigger")
    public Rt<?> triggerJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup, String param) {
        taskJobInfoService.triggerJob(jobName, jobGroup, param);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】运行成功！");
    }

    /**
     * 手动补偿处理
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("resolve")
    public Rt<?> resolveJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup, String param, @NotBlank(message = "执行节点不能为空") String host, @NotBlank(message = "补偿单号不能为空") String serialNo) {
        taskJobInfoService.resolveJob(jobName, jobGroup, serialNo, param, host);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】运行成功！");
    }

}
